package com.example.alex_zhang.addsetting;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;

class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int CONTENT_TYPE = 0;
    public static final int FOOTER_TYPE = 1;
    private final LayoutInflater inflater;
    private List<DataBean> data;
    private Context context;

    public MyAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        data = new ArrayList<>();

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == CONTENT_TYPE) {
            return new ContentViewHolder(inflater.inflate(R.layout.item_content, parent, false));
        }
        return new FooterViewHolder(inflater.inflate(R.layout.item_footer, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {

        if (getItemViewType(position) == CONTENT_TYPE) {
            if (holder instanceof ContentViewHolder) {
                final DataBean dataBean = data.get(position);
                final EditText etTitle = ((ContentViewHolder) holder).etTitle;
                etTitle.setText(dataBean.getTitle());
                etTitle.setOnFocusChangeListener((ContentViewHolder) holder);
                etTitle.addTextChangedListener(new MyTextWatcher() {
                    @Override
                    public void afterTextChanged(Editable s) {
                        super.afterTextChanged(s);
                        dataBean.setTitle(s.toString());
                    }
                });


                final EditText etContent = ((ContentViewHolder) holder).etContent;
                etContent.setText(dataBean.getContent());
                etContent.setOnFocusChangeListener((ContentViewHolder) holder);
                etContent.addTextChangedListener(new MyTextWatcher() {
                    @Override
                    public void afterTextChanged(Editable s) {
                        super.afterTextChanged(s);
                        dataBean.setContent(s.toString());
                    }
                });

                ((ContentViewHolder) holder).albumAdapter.setData(data.get(position).getAlbumUrls());

            }
        } else {
            if (holder instanceof FooterViewHolder) {
                ((FooterViewHolder) holder).add.setOnClickListener((FooterViewHolder) holder);
            }
        }
    }

    @Override
    public int getItemCount() {
        return data == null ? getFooterCount() : data.size() + getFooterCount();
    }

    private void setFooterCount() {

    }

    private int getFooterCount() {
        return 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1) {
            return FOOTER_TYPE;
        }
        return CONTENT_TYPE;
    }

    private void setData(List<DataBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    void onActivityResult(ArrayList<String> data) {
        DataBean dataBean = this.data.get(mPosition);
        dataBean.setAlbumUrls(data);
        notifyItemChanged(mPosition);
    }

    private int mPosition;

    class ContentViewHolder extends RecyclerView.ViewHolder implements View.OnFocusChangeListener, OnSelectItemClickListener {

        private final EditText etTitle;
        private final EditText etContent;
        private final RecyclerView album;
        private AlbumAdapter albumAdapter;


        ContentViewHolder(@NonNull View itemView) {
            super(itemView);
            etTitle = itemView.findViewById(R.id.et_title);
            etContent = itemView.findViewById(R.id.et_content);
            album = itemView.findViewById(R.id.tv_grid_pic);
            albumAdapter = new AlbumAdapter(context, this);
            albumAdapter.setOnSelectItemClickListener(this);
            album.setAdapter(albumAdapter);
            album.setLayoutManager(new GridLayoutManager(context, 5));
        }

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (v.getId() == R.id.et_title) {

            } else if (v.getId() == R.id.et_content) {

            }
        }


        @Override
        public void onSelectItemClickListener(int position) {
            mPosition = position;
        }
    }


    private void add() {
        DataBean dataBean = new DataBean();
        data.add(dataBean);
        notifyItemInserted(data.size());
    }

    public List<DataBean> getData() {
        return data;
    }

    class FooterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private final Button add;

        FooterViewHolder(@NonNull View itemView) {
            super(itemView);
            add = itemView.findViewById(R.id.btn_add);
        }

        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.btn_add) {
                add();
            }
        }
    }
}
